package com.chefu.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.admin.pojo.ProviderUser;
import com.chefu.admin.pojo.ProviderVo;
import com.chefu.admin.pojo.ServiceProvider;
import com.chefu.admin.service.PartProviderService;
import com.chefu.admin.service.ProviderAdminService;
import com.chefu.admin.service.ProviderUserService;
import com.chefu.admin.service.ServiceProviderService;
import com.chefu.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProviderAdminServiceImpl implements ProviderAdminService {

    @Autowired
    ServiceProviderService serviceProviderService;
    @Autowired
    ProviderUserService userService;
    @Autowired
    PasswordEncoder passwordEncoder;


    /**
     * 新增厂商主体
     */
    @Override
    public void add(ServiceProvider serviceProvider) {
        String origin = serviceProvider.getTChain() ? "连锁添加" : "B2C注册";
        serviceProvider.setTOrigin(origin);//来源
        serviceProviderService.save(serviceProvider);
    }


    /**
     * 分页查询厂商列表
     * @param check 审核状态（0：未通过 1：通过 2：拒绝）
     * @param name 厂商简称
     * @param code 主体编码（id）
     * @param city 主体区域id
     * @param phone 手机
     * @param tChain 连锁状态
     * @param status 锁定状态
     * @return
     */
    @Override
    public PageResult<ServiceProvider> findByPage(Long pageNo, Long pageSize, Integer check, String name, String code, Long city, String phone, Boolean tChain, Integer status) {
        QueryWrapper<ServiceProvider> queryWrapper = new QueryWrapper<>();
        if (null != check){
            queryWrapper.eq("t_check", check);
        }
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("short_name", name);
        }
        if (StringUtils.isNotEmpty(code)){
            queryWrapper.eq("sp_id", code);
        }
        if (null != city){
            queryWrapper.eq("area_id", city);
        }
        if (StringUtils.isNotEmpty(phone)){
            queryWrapper.eq("phone", phone);
        }
        if (null != tChain){
            queryWrapper.eq("t_chain", tChain);
        }
        if (null != status){
            queryWrapper.eq("status", status);
        }

        Page<ServiceProvider> page = new Page<>();
        serviceProviderService.page(page, queryWrapper);

        return new PageResult<>(page.getTotal(), page.getPages(), page.getRecords());
    }

    /**
     * 修改厂商主体
     */
    @Override
    public void update(ServiceProvider serviceProvider) {
        serviceProviderService.updateById(serviceProvider);
    }

    /**
     * 锁定厂商
     */
    @Override
    public void updateStatus(Long spId, Integer status) {
        ServiceProvider serviceProvider = new ServiceProvider();
        serviceProvider.setSpId(spId);
        serviceProvider.setStatus(status);
        serviceProviderService.updateById(serviceProvider);
    }

    /**
     * 修改厂商审核状态
     */
    @Override
    public void updateCheck(Long id, Integer check) {
        ServiceProvider serviceProvider = new ServiceProvider();
        serviceProvider.setSpId(id);
        serviceProvider.setTCheck(check);
        serviceProviderService.updateById(serviceProvider);
    }

    /**
     * 创建厂商管理员
     */
    @Override
    public void addUser(ProviderUser providerUser) {
        if (StringUtils.isEmpty(providerUser.getPhone())){
            //根据主体信息初始化姓名、手机
            ServiceProvider provider = serviceProviderService.getById(providerUser.getSpId());
            providerUser.setRealName(provider.getContact());
            providerUser.setPhone(provider.getPhone());
        }
        userService.save(providerUser);
    }


    /**
     * 查看主体用户列表
     */
    @Override
    public PageResult<ProviderUser> findProviderUserByPage(Long pid, Long pageNo, Long pageSize,
                                                           String username, String realName, String phone) {
        QueryWrapper<ProviderUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sp_id", pid);
        if (StringUtils.isNotEmpty(username))
            queryWrapper.like("username", username);
        if (StringUtils.isNotEmpty(realName))
            queryWrapper.like("real_name",realName);
        if (StringUtils.isNotEmpty(phone))
            queryWrapper.eq("phone", phone);

        Page<ProviderUser> page = new Page<>();
        userService.list(queryWrapper);
        List<ProviderUser> records = page.getRecords();
        records.forEach(user -> user.setPassword(null));
        return new PageResult<>(page.getTotal(), page.getPages(), records);
    }

    /**
     * 重置厂商用户密码
     */
    @Override
    public void resetUserPassword(Long id) {
        ProviderUser user = new ProviderUser();
        user.setPuserId(id);
        user.setPassword(passwordEncoder.encode("123456"));
        userService.updateById(user);
    }
}
